#Project: TV polarization
#Author: Josh Ryan and Jeff Lyons
#Working Date: Winter 23
#Task: Use Imai, Kim, Wang PanelMatch process to display treatment timing
#Related files: chamber_level_forstata_v4_102524.dta is loaded, panelmatch_display_1 and panelmatch_display_2 are saved as pdf


library(foreign)
library(effects)
library(sciplot)
library(sandwich)
library(lmtest)   
library(BMS)
library(rJava)
library(lattice)
library(plyr)
library(lme4)
library(gmodels)
library(MASS)
library(stringr)
library("XLConnect")
library(reshape2)
library(xlsx)
library(dplyr)
library(doBy)
library(Hmisc)
library(stargazer)
library(xtable)
library(dplyr)
library(xtable)
library(foreign)
library(dotwhisker)#https://cran.r-project.org/web/packages/dotwhisker/vignettes/dotwhisker-vignette.html
library(berryFunctions) #for the insert row function
library(ggplot2)
library(gridExtra)
library(wnominate)
library(tidyr)
library(wnominate)
library(oc)
library(haven)
library(PanelMatch)

dat<-read_dta("chamber_level_forstata_v4_102524.dta")

#################IMPORTANT NOTE: THE NEW VERSION OF PANELMATCH DOES NOT APPEAR TO SUPPORT STRING LABELS (I.E., THE STATE CHAMBERS) IN THIS FIGURE. THIS WAS CREATED WITH PANELMATCH V.1.0.0 WHICH DOES. UNINSTALL AND REINSTALL THIS OLD VERSION OF PANELMATCH TO RUN THIS CODE.


####Figure A1


#Have to keep making as dataframe because of the way Panelmatch works
dat<-as.data.frame(dat)

#create state-chamber id that is character to use as labels--also because Panelmatch is picky
dat$state_cham2<-paste(dat$state, dat$Chamber,sep = "-")


#####Display Treatment
#because there are so many rows, I am going to cut the data in half to display two graphs

#need to order by year adopted
adopted<-subset(dat, treatment==1)
adopted<-aggregate(year~state_cham,adopted,min)
adopted <- adopted[order(adopted$year),]
adopted$rownum<-seq_along(adopted[,1])

#now merge back into dataframe
dat<-merge(dat, adopted, by=c("year", "state_cham"), all.x = TRUE)

#have to pull rownumber up
dat<-dat %>%
  group_by(state_cham) %>%
  fill(rownum,.direction ="updown")

#now add in MO and NC because they are filled with 0--Code them as 99 because they are at the very bottom of the graph, and I separate by rowcount
dat$rownum[dat$state=="NC" & dat$Chamber=="Senate"]<-99
dat$rownum[dat$state=="SD" & dat$Chamber=="House"]<-99
dat$rownum[dat$state=="SD" & dat$Chamber=="Senate"]<-99

#dat$rownum[dat$state=="MO" & dat$Chamber=="Senate"]<-99

#for first graph, cut in half all rows before 44
dat.1<-subset(dat, rownum<46)
dat.1<-as.data.frame(dat.1)
dat.1$year<-as.integer(dat.1$year)


#display treatment for visualization of variation within and across units
displaytreat<-DisplayTreatment(unit.id = "state_cham2",
                               time.id = "year", legend.position = "none",
                               xlab = "Year", ylab = "", y.size=NULL, x.size=NULL,
                               treatment = "treatment", color.of.treated = "goldenrod1",
                               color.of.untreated = "burlywood4", hide.y.tick.label=FALSE, title="",
                               data = dat.1)

plot(displaytreat)

displaytreat<-displaytreat +theme(text = element_text(size=11),
                                  axis.ticks = element_blank())

plot(displaytreat) #save by hand as pdf--remember to change device size

##############33now for part 2
#everything above row 43
dat.2<-subset(dat, rownum>44)
dat.2<-as.data.frame(dat.2)
dat.2$year<-as.integer(dat.2$year)

#display treatment for visualization of variation within and across units
displaytreat<-DisplayTreatment(unit.id = "state_cham2",
                               time.id = "year", legend.position = "none",
                               xlab = "Year", ylab = "", y.size=NULL, x.size=NULL,
                               treatment = "treatment", color.of.treated = "goldenrod1",
                               color.of.untreated = "burlywood4", hide.y.tick.label=FALSE, title="",
                               data = dat.2)

plot(displaytreat)

displaytreat<-displaytreat +theme(text = element_text(size=11),
                                  axis.ticks = element_blank())

plot(displaytreat) #save by hand as pdf

